#include "stdafx.h"
#include "Thread.h"
#include <process.h>


CThread::CThread()
{
	m_hThread = NULL;
	m_nThreadID = 0;
	m_ThreadParam.pEntry = NULL;
	m_ThreadParam.pParam = NULL;
}


CThread::~CThread()
{
}

bool CThread::Start(PTHREADENTRY pFunc, LPVOID pParam)
{
	m_ThreadParam.pEntry = pFunc;
	m_ThreadParam.pParam = pParam;

	int ret = static_cast<int>(_beginthreadex(NULL, 0, BaseThreadEntry, &m_ThreadParam, 0, &m_nThreadID));

	if(ret > 0)
	{
		m_hThread = reinterpret_cast<HANDLE>(ret);
		return true;
	}
	else
	{
		return false;
	}

}

bool CThread::Suspend()
{
	return (SuspendThread(m_hThread) != 0xffffffff);
}

bool CThread::Resume()
{
	return (ResumeThread(m_hThread) != 0xffffffff);
}

void CThread::Terminate(DWORD dwExitCode)
{
	TerminateThread(m_hThread, dwExitCode);
}

unsigned __stdcall CThread::BaseThreadEntry(LPVOID pParam)
{
	_ThreadParam *pThreadParam = (_ThreadParam *) pParam;

	DWORD dwRet = pThreadParam->pEntry(pThreadParam->pParam);

	return (unsigned)dwRet;
}

